package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ghck.server.model.PatientEventExt;
import com.ghck.server.model.query.PatientEventExtQuery;
/**
 * 用户健康数据扩展表  <br>
 * t_patient_event_ext <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.PatientEventExtMapper
 */
 public class PatientEventExtProvider {
 
 	/**
 	* 新增 用户健康数据扩展表
 	*/
 	public String insert(PatientEventExt domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_patient_event_ext(create_time,last_modify_time");
	 	if(domain.getParentId()!=null){
			sb.append(",parent_id");
		}
	 	if(domain.getCategoryId()!=null){
			sb.append(",category_id");
		}
	 	if(domain.getTimeBlockId()!=null){
			sb.append(",time_block_id");
		}
	 	if(domain.getBloodSugar()!=null){
			sb.append(",blood_sugar");
		}
	 	if(domain.getBloodOxygen()!=null){
			sb.append(",blood_oxygen");
		}
	 	if(domain.getSystolicPressure()!=null){
			sb.append(",systolic_pressure");
		}
	 	if(domain.getDiastolicPressure()!=null){
			sb.append(",diastolic_pressure");
		}
	 	if(domain.getTemperature()!=null){
			sb.append(",temperature");
		}
	 	if(domain.getHeartRate()!=null){
			sb.append(",heart_rate");
		}
	 	if(domain.getBodyAge()!=null){
			sb.append(",body_age");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getParentId()!=null){
			sb.append(",#{parentId}");
		}
	 	if(domain.getCategoryId()!=null){
			sb.append(",#{categoryId}");
		}
	 	if(domain.getTimeBlockId()!=null){
			sb.append(",#{timeBlockId}");
		}
	 	if(domain.getBloodSugar()!=null){
			sb.append(",#{bloodSugar}");
		}
	 	if(domain.getBloodOxygen()!=null){
			sb.append(",#{bloodOxygen}");
		}
	 	if(domain.getSystolicPressure()!=null){
			sb.append(",#{systolicPressure}");
		}
	 	if(domain.getDiastolicPressure()!=null){
			sb.append(",#{diastolicPressure}");
		}
	 	if(domain.getTemperature()!=null){
			sb.append(",#{temperature}");
		}
	 	if(domain.getHeartRate()!=null){
			sb.append(",#{heartRate}");
		}
	 	if(domain.getBodyAge()!=null){
			sb.append(",#{bodyAge}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 用户健康数据扩展表
 	*/
	public String update(PatientEventExt domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_patient_event_ext set last_modify_time = now() ");
		
		if(domain.getParentId()!=null){
			sb.append(",parent_id=#{parentId}");
		}
		if(domain.getCategoryId()!=null){
			sb.append(",category_id=#{categoryId}");
		}
		if(domain.getTimeBlockId()!=null){
			sb.append(",time_block_id=#{timeBlockId}");
		}
		if(domain.getBloodSugar()!=null){
			sb.append(",blood_sugar=#{bloodSugar}");
		}
		if(domain.getBloodOxygen()!=null){
			sb.append(",blood_oxygen=#{bloodOxygen}");
		}
		if(domain.getSystolicPressure()!=null){
			sb.append(",systolic_pressure=#{systolicPressure}");
		}
		if(domain.getDiastolicPressure()!=null){
			sb.append(",diastolic_pressure=#{diastolicPressure}");
		}
		if(domain.getTemperature()!=null){
			sb.append(",temperature=#{temperature}");
		}
		if(domain.getHeartRate()!=null){
			sb.append(",heart_rate=#{heartRate}");
		}
		if(domain.getBodyAge()!=null){
			sb.append(",body_age=#{bodyAge}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<PatientEventExt>> map){
		StringBuffer sb = new StringBuffer();
		List<PatientEventExt> patientEventExts = map.get("list");
		sb.append("insert into t_patient_event_ext(create_time,last_modify_time,parent_id,category_id,time_block_id,blood_sugar,blood_oxygen,systolic_pressure,diastolic_pressure,temperature,heart_rate,body_age) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].parentId}"
	    		+ ",#'{'list[{0}].categoryId}"
	    		+ ",#'{'list[{0}].timeBlockId}"
	    		+ ",#'{'list[{0}].bloodSugar}"
	    		+ ",#'{'list[{0}].bloodOxygen}"
	    		+ ",#'{'list[{0}].systolicPressure}"
	    		+ ",#'{'list[{0}].diastolicPressure}"
	    		+ ",#'{'list[{0}].temperature}"
	    		+ ",#'{'list[{0}].heartRate}"
	    		+ ",#'{'list[{0}].bodyAge}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(patientEventExts)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  patientEventExts.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 用户健康数据扩展表
 	*/
	public String findByQuery(PatientEventExtQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_patient_event_ext a ");
		
		//TODO: add your logic
		
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by create_time desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(PatientEventExtQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_patient_event_ext a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 用户健康数据扩展表
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_patient_event_ext where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	